package com.airbnb.mvrx.sample.features.book.views

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.support.v7.graphics.Palette
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.airbnb.epoxy.CallbackProp
import com.airbnb.epoxy.ModelProp
import com.airbnb.epoxy.ModelView
import com.example.mvrxstudy.R
import com.facebook.common.executors.UiThreadImmediateExecutorService
import com.facebook.common.references.CloseableReference
import com.facebook.datasource.DataSource
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber
import com.facebook.imagepipeline.image.CloseableImage
import com.facebook.imagepipeline.request.ImageRequestBuilder


@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT)
class BookDetailHeader @JvmOverloads constructor(context: Context) : FrameLayout(context) {

    private val mThumb: SimpleDraweeView
    private val mBookName: TextView
    private val mAuthor: TextView
    private val mPublisher: TextView
    private val mBgPalette: View by lazy { findViewById<View>(R.id.v_bg_palette) }
    private var mPaletteCallback: PaletteCallback? = null

    init {
        inflate(context, R.layout.view_header_book_detail, this)
        mThumb = findViewById(R.id.iv_image)
        mBookName = findViewById(R.id.tv_book_name)
        mAuthor = findViewById(R.id.tv_author)
        mPublisher = findViewById(R.id.tv_publisher)
    }

    @CallbackProp
    fun setPaletteCallback(callback: PaletteCallback?) {
        mPaletteCallback = callback
    }


    @ModelProp
    fun setThumb(thumb: String) {
//        mThumb.setImageURI(thumb)
        val dataSubscriber = object : BaseBitmapDataSubscriber() {
            override fun onNewResultImpl(bitmap: Bitmap?) {
                mThumb.setImageBitmap(bitmap)
                var bitmap = bitmap
                bitmap = bitmap!!.copy(bitmap.config, true)
                Palette.from(bitmap).generate { palette ->
                    val rgb = palette.mutedSwatch?.rgb!!
                    mBgPalette.setBackgroundColor(rgb)
                    mPaletteCallback!!.getRgb(rgb)

                }
            }

            override fun onFailureImpl(dataSource: DataSource<CloseableReference<CloseableImage>>) {
            }
        }
        val builder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(thumb))
        val imageRequest = builder.build()

        val imagePipeline = Fresco.getImagePipeline()
        val dataSource = imagePipeline.fetchDecodedImage(imageRequest, null)
        dataSource.subscribe(dataSubscriber, UiThreadImmediateExecutorService.getInstance())
    }

    @ModelProp
    fun setBookName(bookName: String) {
        mBookName.text = bookName
    }

    @ModelProp
    fun setAuthor(author: String) {
        mAuthor.text = author
    }

    @ModelProp
    fun setPublisher(publisher: String) {
        mPublisher.text = publisher
    }

    public interface PaletteCallback {
        fun getRgb(rgb: Int)
    }

}